package cn.yza.并发.CyclicBarrier;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @author Honglixi
 * @create 2021-10-31 21:36
 */
public class Horse implements Runnable{

    private static int counter = 0;
    private final int id = counter++; //第几只马
    private int strides = 0;
    private static Random rand = new Random(40);
    private static CyclicBarrier barrier;

    public Horse(CyclicBarrier b) {
        barrier = b;
    }

    public int getStrides() {
        return strides;
    }

    @Override
    public void run() {
        try {
            //没有中断就不断循环
            while (!Thread.interrupted()) {
                synchronized (this) {
                    //马的步数
                    strides += rand.nextInt(5);
                }
                barrier.await();
            }
        } catch (Exception e) {

            throw new RuntimeException(e);
        }
    }
    @Override
    public String toString() {
        return "Horse " + id + " ";
    }

    public String tracks() {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < getStrides(); i++) {
            s.append("*");
        }
        s.append(id);
        return s.toString();
    }
}
class HorseRace {
    static final int FINISH_LINE = 75;
    private List<Horse> horses = new ArrayList<Horse>();
    private ExecutorService exec = Executors.newCachedThreadPool();
    private CyclicBarrier barrier;

    public HorseRace(int nHorses, final int pause) {
        //初始化循环屏障
        barrier = new CyclicBarrier(nHorses, new Runnable() {
            // 循环多次执行的任务
            @Override
            public void run() {

                // The fence on the racetrack
                StringBuilder s = new StringBuilder();
                for (int i = 0; i < FINISH_LINE; i++) {
                    s.append("=");
                }
                System.out.println(s);

                //打印马移动距离
                for (Horse horse : horses) {
                    System.out.println(horse.tracks());
                }

                //判断有没有马到终点了
                for (Horse horse : horses) {
                    if (horse.getStrides() >= FINISH_LINE) {
                        System.out.println(horse + "won!");
                        exec.shutdownNow();// 有只马跑赢了，所有任务都结束了
                        return;
                    }
                }

                try {
                    TimeUnit.MILLISECONDS.sleep(pause);
                } catch (InterruptedException e) {
                    System.out.println("barrier-action sleep interrupted");
                }
            }
        });
        // 开跑！
        for (int i = 0; i < nHorses; i++) {
            Horse horse = new Horse(barrier);
            horses.add(horse);
            exec.execute(horse);
        }
    }

    public static void main(String[] args) {
        int nHorses = 7;
        int pause = 200;
        new HorseRace(nHorses, pause);
    }
}